﻿using cpe.cpein.SERVER.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cpe.cpein.SERVER.Tools
{
    public static partial class ObjectConverter
    {
        /// <summary>
        /// Function used to convert a WS Message to a Data Message.
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public static DATA.Message MessageConverter(Message Message)
        {
            if (Message == null)
                return null;

            return new DATA.Message()
            {
                msg_id = Message.Id,
                msg_content = Message.Content,
                msg_date = Message.Date,
                msg_isprivate = Message.IsPrivate,
                msg_receiver_del = Message.ReceiverDel,
                msg_receiver_read = Message.ReceiverRead,
                msg_sender_del = Message.SenderDel,
                msg_subject = Message.Subject,
                usr_receiver_id = Message.IDReceiver,
                usr_sender_id = Message.IDSender,
                msg_isdeleted = Message.IsDeleted,
            };
        }

        /// <summary>
        /// Function used to convert a Data Message to a WS Message.
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public static Message MessageConverter(DATA.Message Message)
        {
            if (Message == null)
                return null;

            User sender = Tools.ObjectConverter.UserConverter(BUSINESS.BUUsers.GetUserByID(Message.usr_sender_id));
            User receiver = Tools.ObjectConverter.UserConverter(BUSINESS.BUUsers.GetUserByID(Message.usr_receiver_id));

            return new Message()
            {
                Id = Message.msg_id,
                Content = Message.msg_content,
                IsPrivate = Message.msg_isprivate,
                Date = DateTime.Parse(Message.msg_date.ToString()),
                IDReceiver = Message.usr_receiver_id,
                IDSender = Message.usr_sender_id,
                Sender = sender,
                Receiver = receiver,
                SenderDel = Message.msg_sender_del,
                ReceiverDel = Message.msg_receiver_del,
                Subject = Message.msg_subject,
                ReceiverRead = Message.msg_receiver_read,
                IsDeleted = Message.msg_isdeleted,
            };
        }

        /// <summary>
        /// Function used to convert a list of Data Messages to a List of WS Messages.
        /// </summary>
        /// <param name="Messages"></param>
        /// <returns></returns>
        public static List<Message> MessageConverter(List<DATA.Message> Messages)
        {
            List<Message> res = new List<Message>();
            foreach (DATA.Message us in Messages)
                res.Add(Tools.ObjectConverter.MessageConverter(us));

            return res;
        }

        /// <summary>
        /// Function used to convert a list of WS Messages to a List of Data Messages.
        /// </summary>
        /// <param name="Messages"></param>
        /// <returns></returns>
        public static List<DATA.Message> MessageConverter(List<Message> Messages)
        {
            List<DATA.Message> res = new List<DATA.Message>();
            foreach (Message us in Messages)
                res.Add(Tools.ObjectConverter.MessageConverter(us));

            return res;
        }
    }
}